package com.sduept.nwld.dataserver.manager.etl;

import com.alibaba.fastjson.JSON;
import com.sduept.bigdata.fault.entity.RequestUnfinishedTask;
import com.sduept.core.dao.SuperDAO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class RequestUnfinishTaskETLManager {
	
	@Autowired
	private SuperDAO dao;
	
	public  void receiveAndSaveJsonData(String json) {
		RequestUnfinishedTask task = JSON.parseObject(json,RequestUnfinishedTask.class);
		if(task.getType() == 2) {//手动停止告警消息
			RequestUnfinishedTask task2 = this.findOngoingByRequestIdAndType(task.getRequestId(), 2);//查询
			if(task2 != null) {//更新
				task2.setStatus(1);
				task2.setLastUpdateTime(task.getLastUpdateTime());
				dao.createOrUpdate(task2, RequestUnfinishedTask.class);

			}else {
				dao.createOrUpdate(task, RequestUnfinishedTask.class);
			}
		}else {
			dao.createOrUpdate(task, RequestUnfinishedTask.class);
		}
		log.info("更新数据源告警信息");
	}

	/**
	 * 根据请求id、告警类型查询告警中的数据
	 * @param requestId
	 * @param type
	 * @return
	 */
	public RequestUnfinishedTask findOngoingByRequestIdAndType(String requestId, int type) {
		RequestUnfinishedTask requestUnfinishedTask = null;
		String whereHql = "requestId = ? and type = ? and status = 0";
		List<RequestUnfinishedTask> requestList = dao.findByCondition(RequestUnfinishedTask.class, whereHql, requestId, type);
		if(requestList.size() > 0) {
			requestUnfinishedTask = requestList.get(0);
		}
		return requestUnfinishedTask;
	}

}
